<?php

namespace App\Http\Controllers\Home\Index;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Home\Index\Api\IndexApi;
use App\Http\Controllers\Home\Index\Api\HotDataLoad;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\CommonApi\CommonApi;

class IndexController extends Controller
{   
    /**
     * 显示首页
     * @author wuboheng gzstar117@aliyun.com
     * @return 加载首页视图,并且把数据传递到首页
     */
    public function index()
    {   
        //=========  下面这些数据已通过ShareIndexDataProvider  服务提供者分享到 /Home/Index/index视图
        // $indexApi = new IndexApi();

        // // //查询商品根分类, 用于放置在横向导航栏
        // $sortList = $indexApi->loadSort();

        // // //查询轮播图
        // $carousels = $indexApi->loadCarousels();

        // // //查询轮播图隔壁促销图片
        // $asidePromotion = $indexApi->loadAsidePromotion();

        // // //查询特惠促销栏目的信息
        // $homePromotion = $indexApi->loadHomePromotion(3, 2);

        // // //查询热卖促销栏目的信息
        // $hotSale = $indexApi->loadHotSale();
        // =============================================

        //当键名不存在时候,执行把热销top30商品以哈希类型存入Redis的函数
        if (!Redis::hLen('hotData')) {

            $hotDataApi = new HotDataLoad();
            $hotDataApi->loadHotData();
        }

        //test用,要删掉
        // $hotDataApi = new HotDataLoad();
        // $hotDataApi->loadHotData();

        return view('/Home/Index/index', [

            // 'sortList' => $sortList,
            // 'carousels' => $carousels,
            // 'asidePromotion' => $asidePromotion,
            // 'homePromotion' => $homePromotion,
            // 'hotSale' => $hotSale,

        ]);

    }


    /**
     * 根据用户点击的商品类别,显示所含有的所有商品
     * @author 吴伯恒
     * @return [View] [找出属于该商品类别的所有商品,并且把商品信息传递到商品列表页]
     */
    public function listByCategory(Request $request)
    {   
        //获取传过来的
        $sortID = $request->input('sortid');

        //调用IndexApi的findGoodsBySort()函数,递归获取改商品类别底下的所有商品
        $indexApi = new IndexApi();
        $goods = $indexApi->findGoodsBySort($sortID);

        // dd($goods);

        //判断是否有价格范围的限制,筛选商品
        $from = $request->input('from');
        $to = $request->input('to');
        if ($from >= 0 && $to > 0 && $from <= $to) {
            
            //如果超出价格范围的,则删除该商品
            foreach ($goods as $k => $v) {
                if ($v['price'] < $from || $v['price'] > $to) {
                    // echo '321';
                    unset($goods[$k]);
                }
            }

        }

        //计算查询到的商品数量
        $count = count($goods);

        //判断是否有排序条件
        switch ($request->input('sort')) {

            //价格降序
            case 'price-desc':
                array_multisort(array_column($goods,'price'),SORT_DESC,$goods);
                break;

            //价格升序
            case 'price-asc':
                array_multisort(array_column($goods,'price'),SORT_ASC,$goods);
                break;

            //销量降序
            case 'sales-desc':
                array_multisort(array_column($goods,'sales'),SORT_DESC,$goods);
                break;

            //人气降序
            case 'hot-desc':
                array_multisort(array_column($goods,'click'),SORT_DESC,$goods);
                break;
            
            default:
                
                break;
        }

        // dd($goods);

        if ($goods) {
            //===================  分页,函数的参数3决定每页显示个数  ============================
            $goods = CommonApi::CustomPagination($request, $goods, 8);
            $msg = ['code' => '200', 'msg' => '查询成功'];

        } else {

            $msg = ['code' => '404', 'msg' => '没有找到相关的商品'];
        }

        // dd($goods);

        return view('Home/List/ListByCategory', [

            'msg' => $msg,
            'sortid' => $sortID,
            'goods' => $goods,
            'count' => $count,
            'sort' => $request->input('sort'),
            'from' => $request->input('from'),
            'to' => $request->input('to'),
            // 'keyword' => '随便填的',

        ]);


    }
}
